package com.mjfeed.controller.shop.member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.Area;
import com.mjfeed.entity.Member;
import com.mjfeed.entity.Receiver;
import com.mjfeed.service.AreaService;
import com.mjfeed.service.MemberService;
import com.mjfeed.service.ReceiverService;
import com.mjfeed.util.QueryCondition;

/**
 * @description 收货地址
 * @author zhaominglei
 * @date 2014年10月8日
 */
@Controller("shopMemberReceiverController")
@RequestMapping("/member/receiver")
public class ReceiverController extends BaseController {
	/** 每页记录数 */
	private static final int PAGE_SIZE = 10;
	
	@Resource(name="memberService")
	private MemberService memberService;
	@Resource(name="receiverService")
	private ReceiverService receiverService;
	@Resource(name="areaService")
	private AreaService areaService;
	
	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		queryConditions.add(new QueryCondition("member", QueryCondition.EQ, member));
		String orderBy = "order by id desc";
		request.setAttribute("receivers", receiverService.get(Receiver.class, queryConditions, orderBy, 1, PAGE_SIZE));
		request.setAttribute("member", member);
		return "shop/member/receiver/list";
	}
	
	@RequestMapping(value = "/add", method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "shop/member/receiver/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(Receiver receiver, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null || member.getId() == null || member.getId() < 1) {
			return ERROR_VIEW;
		}
		String areaId = request.getParameter("areaId");
		if (StringUtils.isEmpty(areaId)) {
			return ERROR_VIEW;
		}
		receiver.setIsDefault((receiver.getIsDefault() != null && receiver.getIsDefault()) ? true : false);
		receiver.setMember(member);
		receiver.setArea(areaService.getById(Area.class, Long.valueOf(areaId)));
		receiverService.save(receiver);
		return "redirect:/member/receiver/list.shtml";
	}
	
	@RequestMapping(value = "/edit", method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("receiver", receiverService.getById(Receiver.class, id));
		return "shop/member/receiver/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(Receiver receiver, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null || member.getId() == null || member.getId() < 1) {
			return ERROR_VIEW;
		}
		String areaId = request.getParameter("areaId");
		if (StringUtils.isEmpty(areaId)) {
			return ERROR_VIEW;
		}
		if (receiver.getId() == null || receiver.getId() < 1) {
			return ERROR_VIEW;
		}
		Receiver pReceiver = receiverService.getById(Receiver.class, receiver.getId());
		pReceiver.setConsignee(receiver.getConsignee());
		pReceiver.setAddress(receiver.getAddress());
		pReceiver.setZipCode(receiver.getZipCode());
		pReceiver.setPhone(receiver.getPhone());
		pReceiver.setIsDefault((receiver.getIsDefault() != null && receiver.getIsDefault()) ? true : false);
		pReceiver.setMember(member);
		pReceiver.setArea(areaService.getById(Area.class, Long.valueOf(areaId)));
		receiverService.update(pReceiver);
		return "redirect:/member/receiver/list.shtml";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> delete(Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> data = new HashMap<String, Object>();
		if (id == null || id < 1) {
			data.put("success", "false");
			data.put("message", "param error");
			return data;
		}
		Receiver pReceiver = receiverService.getById(Receiver.class, id);
		if (pReceiver == null) {
			data.put("success", "false");
			data.put("message", "receiver not exists");
			return data;
		}
		receiverService.delete(pReceiver);
		data.put("success", "true");
		data.put("message", "success");
		return data;
	}
}
